//
//  SYYuejuanYiPiGaiView.m
//  SanYiTeaching
//
//  Created by 郑浩然 on 2019/4/25.
//  Copyright © 2019年 SanYi. All rights reserved.
//

#import "SYYuejuanYiPiGaiView.h"
#import "SYTopLabView.h"
#import "SYYiPiGaiTableViewCell.h"
#import "SYXiuGaiDetailsView.h"

@interface SYYuejuanYiPiGaiView () <UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) SYTopLabView *topLabView;
@property (strong, nonatomic) UITableView *curTableView;
@property (strong, nonatomic) SYXiuGaiDetailsView *xiugaiView;
@property (strong, nonatomic) NSMutableArray *dataListArr;
@property (assign, nonatomic) NSInteger index;
@property (strong, nonatomic) SYNoLabel *noLab;

@end

@implementation SYYuejuanYiPiGaiView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initSubView];
    }
    return self;
}

- (void)initSubView {
    _dataListArr = [NSMutableArray array];
    [self addSubview:self.topLabView];
    [self.topLabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.mas_equalTo(40);
    }];
    
    [self addSubview:self.curTableView];
    [self.curTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self);
        make.top.equalTo(self.topLabView.mas_bottom);
    }];
    __weak typeof(self) weakSelf = self;
    [self.curTableView headerRefreshBlock:^{
        [weakSelf.dataListArr removeAllObjects];
        weakSelf.index = 1;
        [weakSelf requestDataList];
    }];
    
    [self.curTableView fooderRefreshBlock:^{
//        weakSelf.index = weakSelf.dataListArr.count/10 +1;
        [weakSelf requestDataList];
    }];
    
    self.noLab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self];
    
    [self.noLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topLabView.mas_bottom);
        make.left.right.bottom.equalTo(self);
    }];
    
}
//请求数据
- (void)requestDataList {
    if (self.index == 1) {
        [self.dataListArr removeAllObjects];
    }
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI yj_requestCorrectServiceCorrectLogPageIndex:self.index pageSize:30 success:^(NSMutableArray *dataListArr) {
        [weakSelf.curTableView.mj_header endRefreshing];
        if (self.index >1) {
            if (dataListArr.count == 0) {
                [weakSelf.curTableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [weakSelf.curTableView.mj_footer endRefreshing];
            }
            
        } else {
            [weakSelf.curTableView.mj_footer endRefreshing];
        }
        if (dataListArr.count>0) {
            self->_index ++;
        }
        [MBProgressHUD mj_hideHUDForView:self];
        [weakSelf.dataListArr addObjectsFromArray:dataListArr];
        [weakSelf.curTableView reloadData];
        if (weakSelf.dataListArr.count > 0) {
            self.noLab.hidden = YES;
        } else {
            self.noLab.hidden = NO;
        }
    } fail:^(NSError *error) {
        self.noLab.hidden = NO;
        [weakSelf.curTableView.mj_footer endRefreshing];
        [weakSelf.curTableView.mj_header endRefreshing];
        [MBProgressHUD mj_hideHUDForView:self];
        [MBProgressHUD zr_showError:error.message];
    }];
}

#pragma mark - tabl
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataListArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SYYiPiGaiTableViewCell *pgCell = [tableView dequeueReusableCellWithIdentifier:@"SYYiPiGaiTableViewCell" forIndexPath:indexPath];
    if (indexPath.row %2) {
        pgCell.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
    } else {
        pgCell.backgroundColor = [UIColor whiteColor];
    }
    pgCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    pgCell.xuhaoLab.text = SJStringWithFormat(indexPath.row +1);
//    TOP
    pgCell.logModel = self.dataListArr[indexPath.row];
    
    
    return pgCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _xiugaiView = [[SYXiuGaiDetailsView alloc] initWithFrame:CGRectMake(kScreen_WIDTH, 0, kScreen_WIDTH, kScreen_HEIGHT) ];
//    _xiugaiView.backgroundColor = [UIColor redColor];
    [[UIApplication sharedApplication].keyWindow addSubview:_xiugaiView];
    __weak typeof(self) weakSelf = self;
    [UIView animateWithDuration:kAnimate animations:^{
        weakSelf.xiugaiView.frame = CGRectMake(0, 0, kScreen_WIDTH, kScreen_HEIGHT);
    } completion:^(BOOL finished) {
        [weakSelf.xiugaiView requestDataListModel:self.dataListArr[indexPath.row]];
    }];
    
}


#pragma mark - 
- (SYTopLabView *)topLabView {
    if (!_topLabView) {
        _topLabView = [[SYTopLabView alloc]initWithDataNameArr:@[@"序号",@"试卷名称",@"题号",@"总分",@"得分",@"对错",@"批改时间"] width:0.143];
    }
    return _topLabView;
}
- (UITableView *)curTableView {
    if (!_curTableView) {
        _curTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        
        _curTableView.delegate = self;
        _curTableView.dataSource = self;
        _curTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _curTableView.backgroundColor = [UIColor whiteColor];
        //        [_curTableView registerCellWithClassNames:@[@"SYAttendLectureTableViewCell"]];
         [_curTableView registerCellWithNIbNames:@[@"SYYiPiGaiTableViewCell"]];
    }
    return _curTableView;
}

@end













